package com.hengda.museumonline.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.RegisterBean;
import com.hengda.museumonline.http.CallServer;
import com.litesuits.common.assist.Base64;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/9/27 16:42
 * @E-mail：jeffy12138@126.com
 */
public class PhoneRegisterActivity extends BaseActivity implements View.OnClickListener {
    private android.widget.EditText etphone;
    private android.widget.EditText etcode;
    private android.widget.TextView tvcode;
    private android.widget.EditText password;
    private android.widget.EditText repassword;
    private android.widget.EditText usernicename;
    private android.widget.CheckBox checkbox;
    private android.widget.LinearLayout register;
    private android.widget.LinearLayout llemail;
    private android.widget.ImageView ivclose;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_register);
        this.ivclose = (ImageView) findViewById(R.id.iv_close);
        ivclose.setOnClickListener(this);
        this.llemail = (LinearLayout) findViewById(R.id.ll_email);
        llemail.setOnClickListener(this);
        this.register = (LinearLayout) findViewById(R.id.register);
        register.setOnClickListener(this);
        this.checkbox = (CheckBox) findViewById(R.id.checkbox);
        this.usernicename = (EditText) findViewById(R.id.user_nicename);
        this.repassword = (EditText) findViewById(R.id.repassword);
        this.password = (EditText) findViewById(R.id.password);
        this.tvcode = (TextView) findViewById(R.id.tv_code);
        tvcode.setOnClickListener(this);
        this.etcode = (EditText) findViewById(R.id.et_code);
        this.etphone = (EditText) findViewById(R.id.et_phone);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.ll_email:
                finish();
                startActivity(PhoneRegisterActivity.this, EmailRegisterActivity.class);
                break;
            case R.id.register:
                if (checkbox.isChecked()) {
                    register();
                }

                break;
            case R.id.tv_code:
                getCode();
                break;
        }
    }

    private void getCode() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "register/send", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(PhoneRegisterActivity.this).versionName);
        request.add("mobile", String.valueOf(etphone.getText()));
        CallServer.getRequestInstance().add(PhoneRegisterActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void register() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "register/index", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(PhoneRegisterActivity.this).versionName);
        request.add("username", String.valueOf(etphone.getText()));
        request.add("password", Base64.encodeToString(String.valueOf(password.getText()).getBytes(), Base64.DEFAULT));
        request.add("repassword", Base64.encodeToString(String.valueOf(repassword.getText()).getBytes(), Base64.DEFAULT));
        request.add("user_nicename", String.valueOf(usernicename.getText()));
        request.add("code", String.valueOf(etcode.getText()));
        CallServer.getRequestInstance().add(PhoneRegisterActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    RegisterBean registerBean = gson.fromJson(response.get().toString(), RegisterBean.class);
                    if (1 == registerBean.getStatus()) {
                        getToast(registerBean.getMsg());
                        finish();
                        startActivity(PhoneRegisterActivity.this, LoginActivity.class);
                    } else if (0 == registerBean.getStatus()) {
                        getToast(registerBean.getMsg());
                    }
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

            }

            @Override
            public void onFinish(int what) {

            }
        });
    }
}
